package wechat.service.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import wechat.handler.bean.WechatInformationBean;
import wechat.handler.confirmation.Confirmation;
import wechat.handler.work.WechatInformationHandler;

@Controller
@RequestMapping("/Wechat")
public class WechatController {

	private Logger log = LoggerFactory.getLogger(WechatController.class);

	/**
	 * 微信服务器验证
	 */
	@GetMapping
	public void confirmation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("WechatServlet Get");

		String appid = request.getAttribute("appid").toString();
		log.info("WechatServlet Get appid=" + appid);

		PrintWriter printWriter = response.getWriter();

		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");

		log.info("WechatServlet  signature=" + signature + " timestamp=" + timestamp + " nonce=" + nonce);

		if (signature == null && timestamp == null && nonce == null) {
			log.info("WechatServlet Request=" + request.getParameterMap());
			return;
		}

		if (Confirmation.confirm(appid, signature, timestamp, nonce)) {
			printWriter.write(echostr);
			printWriter.flush();
			printWriter.close();
		} else {
			System.out.println("WechatServlet : 服务器验证失败！");
			log.error("WechatServlet : 服务器验证失败！");
		}

	}

	/**
	 * 微信消息接收
	 */
	@PostMapping
	public void message(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("WechatServlet Post");

		String appid = request.getAttribute("appid").toString();
		log.info("WechatServlet Post appid=" + appid);

		String encryptType = request.getParameter("encrypt_type");
		String msgSignature = request.getParameter("msg_signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");

		log.info("WechatServlet  encryptType=" + encryptType + " msgSignature=" + msgSignature + " nonce=" + nonce
				+ " timestamp=" + timestamp);

		response.setCharacterEncoding("UTF-8");
		InputStream input = request.getInputStream();

		WechatInformationBean wechatInformationBean = new WechatInformationBean();
		wechatInformationBean.setDataInputStream(input);
		wechatInformationBean.setEncryptType(encryptType);
		wechatInformationBean.setMsgSignature(msgSignature);
		wechatInformationBean.setNonce(nonce);
		wechatInformationBean.setTimestamp(timestamp);

		WechatInformationHandler wechatInformationHandler = new WechatInformationHandler(appid);
		String process = wechatInformationHandler.process(appid, wechatInformationBean);

		PrintWriter printWriter = response.getWriter();
		printWriter.write(process);
		printWriter.flush();
		printWriter.close();
	}

}
